#!/bin/bash


# Color
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'


# Make sure only root can run our script
[[ $EUID -ne 0 ]] && echo -e "[${red}Error${plain}] This script must be run as root!" && exit 1


ubuntu1804_ali(){
lines=(
'deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse'
'deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse'
'deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse'
'deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse'
'deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse'
'deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse'
'deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse'
'deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse'
'deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse'
'deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse'
)
echo '# aliyun' > /etc/apt/sources.list 
for ((i=1;i<=${#lines[@]};i++ )); do
       line="${lines[$i-1]}"
       #echo -e "${green}${i}${plain}) ${hint}"
	#echo $line
	#echo $line > /etc/apt/sources.list
	`sed -i '$a\'"$line" /etc/apt/sources.list`
	#sed -n '$a\123' /etc/apt/sources.list
   done


}

ubuntu1804_qinghua(){
lines=(
'deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse'
'deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse'
'deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse'
'deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse'
'deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse'
'deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse'
'deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse'
'deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse'
'deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse'
'deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse'
)
echo '# qinghua' > /etc/apt/sources.list 
for ((i=1;i<=${#lines[@]};i++ )); do
       line="${lines[$i-1]}"
       #echo -e "${green}${i}${plain}) ${hint}"
	#echo $line
	#echo $line > /etc/apt/sources.list
	`sed -i '$a\'"$line" /etc/apt/sources.list`
	#sed -n '$a\123' /etc/apt/sources.list
   done

}


#create sources.list bak file
createBak(){
if [ ! -f '/etc/apt/sources.list.bak' ];
then
   echo 'bak sources.list to /etc/apt/sources.list.bak'
   cp /etc/apt/sources.list /etc/apt/sources.list.bak
fi
}



#change apt source
changeSource(){
ver=$(grep -oE  "[0-9.]+" /etc/issue |awk -F '.' '{print $1$2}')
echo "os version "$ver
echo "source type "$1
#array= eval "$""ubuntu$ver""_""$1"
#array = `eval echo '$ubuntu'"ver"`
#eval "$""ubuntu$ver""_""$1"
#echo array
#eval "array=$""ubuntu$ver""_""$1"
#array=`eval echo '$ubuntu'"$ver"'_'"$1"` 
#echo ${array[0]}
#echo ${array[1]}
#for ((i=1;i<=${#array[@]};i++ )); do
#        hint="${array[$i-1]}"
# 	 echo -e "${green}${i}${plain}) ${hint}"
#    done
eval "ubuntu$ver""_""$1"

}


# operations
sources=(
'ali'
'qinghua'
'163'
)

while true
do
echo -e "Please select source:"
for ((i=1;i<=${#sources[@]};i++ )); do
        hint="${sources[$i-1]}"
        echo -e "${green}${i}${plain}) ${hint}"
    done
read -p "Which source you'd select(Default: ${sources[0]}):" pick
    [ -z "$pick" ] && pick=1
    expr ${pick} + 1 &>/dev/null
    if [ $? -ne 0 ]; then
        echo -e "[${red}Error${plain}] Please enter a number"
        continue
    fi
    if [[ "$pick" -lt 1 || "$pick" -gt ${#sources[@]} ]]; then
        echo -e "[${red}Error${plain}] Please enter a number between 1 and ${#sources[@]}"
        continue
    fi
    source=${sources[$pick-1]}
    echo
    echo "---------------------------"
    echo "source = ${source}"
    echo "---------------------------"
    echo
break
done

createBak
changeSource $source

# get last return code
lastcode=$(echo $?)
[[ $? -ne 0 ]] && echo -e "[${red}Error${plain}] Unable to execute apt update because the most recent status code is $lastcode!" && exit 1
apt update

